package com.client;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.log4j.Logger;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.gateway.common.Common;
import com.gateway.core.MyBuffer;
import com.gateway.core.MyProtocol;
import com.gateway.entity.LogicServer;

public class Client {

  private Logger log=Logger.getLogger(this.getClass());

  public static void main(String[] args) {
    for(int i=0; i < 1; i++) {
      new Client().login("USER" + i, "PASSWORD" + i);
    }
    // for(int i=51; i < 100; i++) {
    // new Client().register("USER" + i, "PASSWORD" + i, "EMAIL" + i);
    // }
  }

  public void login(String username, String password) {
    NioSocketConnector connector=new NioSocketConnector();
    DefaultIoFilterChainBuilder chain=connector.getFilterChain();
    chain.addLast("ClientProtocolFilter", new ProtocolCodecFilter(new ClientProtocolCodecFactory(Charset.forName("utf-8"))));
    connector.setHandler(new ClientHandler());
    ConnectFuture cf=connector.connect(new InetSocketAddress("localhost", Common.PORT_GATEWAY));
    cf.awaitUninterruptibly();
    //
    // 封装bodyData
    MyBuffer buffer=MyBuffer.allocate(100);
    buffer.putString(username);
    buffer.putString(password);
    buffer.flip();
    // 封装MyProtocol
    MyProtocol myProtocol=new MyProtocol(Common.REQ_LOGIN_LOGICSERVER, buffer.getIoBuffer());
    //
    cf.getSession().write(myProtocol);
  }

  public void register(String username, String password, String email) {
    NioSocketConnector connector=new NioSocketConnector();
    DefaultIoFilterChainBuilder chain=connector.getFilterChain();
    chain.addLast("ClientProtocolFilter", new ProtocolCodecFilter(new ClientProtocolCodecFactory(Charset.forName("utf-8"))));
    connector.setHandler(new ClientHandler());
    ConnectFuture cf=connector.connect(new InetSocketAddress("localhost", Common.PORT_GATEWAY));
    cf.awaitUninterruptibly();
    //
    // 封装bodyData
    MyBuffer buffer=MyBuffer.allocate(100);
    buffer.putString(username);
    buffer.putString(password);
    buffer.putString(email);
    buffer.flip();
    // 封装MyProtocol
    MyProtocol myProtocol=new MyProtocol(Common.REQ_USER_REGISTER, buffer.getIoBuffer());
    //
    cf.getSession().write(myProtocol);
  }

  public void connectionLogicServer(LogicServer ls) {
    NioSocketConnector connector=new NioSocketConnector();
    DefaultIoFilterChainBuilder chain=connector.getFilterChain();
    chain.addLast("ClientProtocolFilter", new ProtocolCodecFilter(new ClientProtocolCodecFactory(Charset.forName("utf-8"))));
    connector.setHandler(new ClientHandler());
    ConnectFuture cf=connector.connect(new InetSocketAddress(ls.getIp(), ls.getPort()));
    cf.awaitUninterruptibly();
    log.debug("连接GAMESERVER");
    // 封装bodyData
    MyBuffer buffer=MyBuffer.allocate(100);
    buffer.putString("hello logicServer!");
    buffer.flip();
    // 封装MyProtocol
    MyProtocol myProtocol=new MyProtocol(Common.REQ_EACH, buffer.getIoBuffer());
    //
    cf.getSession().write(myProtocol);
  }
}
